package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.vo.SpuInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@Api(tags = "spu管理")
@RequestMapping("/admin/product")
public class SpuController {

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SpuImageService spuImageService;

    /**
     *
     * @param pn
     * @param ps
     * @param c3Id
     * @return
     */
    ///admin/product/1/10
    @ApiOperation("分页查询SPU列表")
    @GetMapping("/{page}/{limit}")
    public Result spuList(@ApiParam("页码") @PathVariable("page") Long pn,
                          @ApiParam("每页大小") @PathVariable("limit") Long ps,
                          @ApiParam("分类id") @RequestParam("category3Id") Long c3Id){

        Page<SpuInfo> page = new Page<>(pn,ps);
        QueryWrapper<SpuInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",c3Id);

        Page<SpuInfo> result = spuInfoService.page(page,queryWrapper);
        return Result.ok(result);
    }


    /**
     * SPU商品保存
     * @param spuInfoVo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfoVo spuInfoVo){
        spuInfoService.saveSpuInfo(spuInfoVo);
        return Result.ok();
    }

    /**
     * 查询spu所有图片
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId") Long spuId){
        QueryWrapper<SpuImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id",spuId);
        List<SpuImage> result = spuImageService.list(queryWrapper);
        return Result.ok(result);
    }
}
